package first.codeTop.byteDance;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Leetcode54
 * @description:
 * @author: 荣燊
 * @create: 2021-07-29 16:43
 **/
public class Leetcode54 {
    // 螺旋矩阵,顺时针螺旋顺序打印矩阵
    public static void main(String[] args) {

    }

    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix.length == 0) {
            return result;
        }
        // 左右指针
        int left = 0;
        int right = matrix[0].length-1;
        // 上下指针
        int low = 0;
        int high = matrix.length-1;
        // 总个数
        int total = (right+1) * (high+1);

        while (left <= right && low <= high) {
            int i = left;
            while (i <= right && total > 0) {
                result.add(matrix[low][i++]);
                total--;
            }
            ++low;

            int j = low;
            while (j <= high && total > 0) {
                result.add(matrix[j++][right]);
                total--;
            }
            --right;

            int m = right;
            while (m >= left && total > 0) {
                result.add(matrix[high][m--]);
                total--;
            }
            --high;

            int n = high;
            while (n >= low && total > 0) {
                result.add(matrix[n--][left]);
                total--;
            }
            ++left;
        }
        return result;
    }
}
